package com.fire.control.system.module.login.controller.web;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fire.control.system.dto.LoginResultDTO;
import com.fire.control.system.dto.UserDTO;
import com.fire.control.system.module.common.controller.BaseController;
import com.fire.control.system.module.login.service.ILoginService;
import com.fire.control.system.standard.BusinessInterfaceStandard;
import com.fire.control.system.statics.Key;
import com.fire.control.system.statics.ResponseMessage;
import com.fire.control.system.support.protocol.comman.ResponseProtocol;
import com.fire.control.system.support.protocol.login.LoginRequestProtocol;
import com.fire.control.system.support.util.MD5Util;

/**
 * @author liuxincheng
 * @ClassName: LoginControl
 * @Description: TODO(登陆控制器)
 * @date 2017年5月5日 上午10:57:46
 */
@Controller
@RequestMapping("web/loginCtr")
public class LoginControl extends BaseController
		implements BusinessInterfaceStandard<LoginRequestProtocol, ResponseProtocol> {
	private static final Log log = LogFactory.getLog(LoginControl.class);

	@Autowired
	private ILoginService loginService;

	@Autowired
	private MD5Util md5Util;

	/**
	 * @author liuxincheng @Title: jumpToLoginPage @Description:
	 *         TODO(跳转到登陆页面) @param @param reqProtocol @param @return
	 *         设定文件 @return ResponseProtocol 返回类型 @throws
	 */
	@RequestMapping(path = "jumpToLoginPage", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol jumpToLoginPage(LoginRequestProtocol reqProtocol,HttpServletRequest request) {
		reqProtocol.setTaskuuid(UUID.randomUUID().toString());
		log.info("login (" + reqProtocol.getTaskuuid() + ") task start ...........");
		ResponseProtocol response = loginService.jumpToLoginPage(reqProtocol,request);
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: login
	 * @Description: TODO(登陆)
	 * @param @param
	 *            t (实体参数)
	 * @param @return
	 * @param @throws
	 * @Exception 返回异常
	 * @return String 返回类型
	 */
	@RequestMapping(path = "loginByIdenCode", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol loginByIdenCode(LoginRequestProtocol reqProtocol,HttpServletRequest request)
			throws Exception {
		reqProtocol.setTaskuuid(UUID.randomUUID().toString());
		log.info("loginByIdenCode (" + reqProtocol.getTaskuuid() + ") task start ...........");
		ResponseProtocol response = loginService.loginByIdenCode(reqProtocol,request);
		LoginResultDTO result = (LoginResultDTO) response.getData();
		if (result != null) {
			UserDTO user = result.getUserDTO();
			request.getSession().setAttribute(Key.session_key_lora_user_id.toString(), user);
		} 
		/*else {
			ResponseMessage.CODE_400.messageToProtocol(response).setMessage("登陆失败!");
		}*/
		// 生成Token
		/*
		 * String str = userDto.getUserId() + UUID.randomUUID().toString();
		 * String token = md5Util.getMD5Str(str); redisService.set("token",
		 * token);
		 */
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: loginBySms
	 * @Description: TODO(发送验证码)
	 * @param @param
	 *            resprotocol
	 * @param @return
	 * @param @throws
	 *            Exception 设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws Exception
	 */
	@RequestMapping(path = "SendSms", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol SendSms(LoginRequestProtocol reqsProtocol) throws Exception {
		reqsProtocol.setTaskuuid(UUID.randomUUID().toString());
		log.info("SendSms (" + reqsProtocol.getTaskuuid() + ") task start ...........");
		ResponseProtocol response = loginService.SendSms(reqsProtocol);
		return response;
	}
	
	/**
	 * @author liuxincheng
	 * @Title: loginOut
	 * @Description: TODO(登出系统)
	 * @param @param reqsProtocol
	 * @param @return
	 * @param @throws Exception    设定文件
	 * @return ResponseProtocol    返回类型
	 * @throws null
	 */
	@RequestMapping(path = "loginOut", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol loginOut(LoginRequestProtocol reqsProtocol,HttpServletRequest request) throws Exception {
		reqsProtocol.setTaskuuid(UUID.randomUUID().toString());
		log.info("SendSms (" + reqsProtocol.getTaskuuid() + ") task start ...........");
		ResponseProtocol response = loginService.loginOut(reqsProtocol,this.getUser(),request);
		return response;
	}
}
